package com.scau.hwadee.examination.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scau.hwadee.examination.entity.Textbook;
import com.scau.hwadee.examination.entity.Textbookadjustmentapplication;
import com.scau.hwadee.examination.entity.Textbookadjustmenttime;
import com.scau.hwadee.examination.mapper.TextbookMapper;
import com.scau.hwadee.examination.mapper.TextbookadjustmentapplicationMapper;
import com.scau.hwadee.examination.mapper.TextbookadjustmenttimeMapper;
import com.scau.hwadee.examination.service.TextbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author YHX
 * @date 2019/7/31 16:06
 * description
 */
@Service
public class TextbookServiceImpl implements TextbookService {

  @Autowired
  private TextbookadjustmenttimeMapper textbookadjustmenttimeMapper;

  @Autowired
  private TextbookadjustmentapplicationMapper textbookadjustmentapplicationMapper;

  @Autowired
  private TextbookMapper textbookMapper;

  private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

  @Override
  public int adjustTextbook(JSONArray jsonArray) throws Exception {
    List<Textbookadjustmenttime> textbookadjustmenttimes = new ArrayList<>();
    for (int i = 0; i < jsonArray.size(); i++) {
      JSONObject data = jsonArray.getJSONObject(i);
      Textbookadjustmenttime t = new Textbookadjustmenttime();
      t.setScId(data.get("schoolId").toString());
      t.setTbatEndtime(Date.valueOf(data.get("startTime").toString()));
      t.setTbatEndtime(Date.valueOf(data.get("endTime").toString()));
      textbookadjustmenttimes.add(t);
    }
    try {
      return textbookadjustmenttimeMapper.insertList(textbookadjustmenttimes);
    } catch (Exception e) {
      return 0;
    }
  }

  @Override
  public int adjustTextbookBySchool(Textbookadjustmentapplication t) {
    return textbookadjustmentapplicationMapper.insert(t);
  }

  @Override
  public PageInfo<Textbook> getAllTextbook(int paegNum, int pageSize) {
    PageHelper.startPage(paegNum, pageSize);
    return new PageInfo<>(textbookMapper.selectAllTextbook(paegNum, pageSize));
  }
}
